/**
 * @author Modified by Abdul Khader Azeem
 *
 */

package edu.nus.iss.t5.jass.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import edu.nus.iss.t5.jass.entity.Entity;
import edu.nus.iss.t5.jass.exception.DAOException;
import edu.nus.iss.t5.jass.exception.DuplicateEntityException;



public abstract class IGenericDAO {
  private Properties props;         
  private String fileName;					
  private String primaryKeyVar;   	
  private HashMap<String, Entity> object;  
  
  private int maxEntityIndex= -1;        
	
  public IGenericDAO()
  {
	  props = new Properties();
	  fileName = null;
	  primaryKeyVar = null;
	  object = new HashMap<String, Entity>();
  }
  
  public IGenericDAO(String fileName, String primaryKeyVar) throws IOException
  {
	  props = new Properties();
	  
	  File f = new File(fileName);
	  if (f.exists()){
	  String filePath = f.getAbsolutePath();	  
	  this.fileName = filePath;
	  FileInputStream in = new FileInputStream(this.fileName);
	  
	  props.load(in);
	  in.close();
	  this.primaryKeyVar = primaryKeyVar;
	  object = new HashMap<String, Entity>();

	  getMaxIndex();
	  }else{         
          System.out.println("File not found!");
        }
  }
  
  public abstract void update(Entity e) throws DAOException, IOException;
  
  public abstract void remove(Entity e) throws DAOException, IOException;
  
  public abstract Entity get(String id) throws ParseException;
  
  public abstract Entity[] getAll();
  
  public abstract void save(Entity e) throws DAOException, IOException, DuplicateEntityException;
  
  private void getMaxIndex(){
		Properties props = getProperties();
		Set<Object> keys = props.keySet();
		Iterator itr = keys.iterator();
		String key;
		while(itr.hasNext()) { 
			key = (String)itr.next();
			if(key.startsWith(primaryKeyVar)) {
				String str = key.substring(primaryKeyVar.length());
				int val = Integer.parseInt(str);
                if(val > maxEntityIndex)
                	maxEntityIndex = val;
			}  
		} 
	}
  
	/**
	 * Given a id get the index at which this ID is located in the properties file.
	 * @param id
	 * @return
	 */
	protected int getKeyForId(String id) {		

		Set<Object> keys = props.keySet();
		Iterator itr = keys.iterator();
		String key;
		int int_key = -1;
		while(itr.hasNext()) { 
			key = (String) itr.next(); 
			if(key.startsWith(primaryKeyVar)) {
				String value = props.getProperty(key);
				if(value.compareTo(id) == 0) {
					String str = key.substring(primaryKeyVar.length());
					int_key = Integer.parseInt(str);
					break;
				}
			}
		}
		return int_key;					
	}
	
  protected String getFileName(){
	  return fileName;
  }
  
  protected String getprimaryKeyVar(){
	  return primaryKeyVar;
  }
  
  protected int getMaxEntityIndex(){
	  return maxEntityIndex;
  }
  
  protected int getNextMaxEntityIndex(){
	  return ++maxEntityIndex;
  }
  
  protected void store() throws IOException {
	  FileOutputStream out = new FileOutputStream(fileName);
	  props.store(out, "Storing");
  }
  
  protected Properties getProperties() {
	return props;
  }
  
  protected HashMap<String, Entity> getObject() {
	  return object;
  }
  
  public int size() {
		Properties props = getProperties();
		Set<Object> keys = props.keySet();
		Iterator itr = keys.iterator();
		String key;
		int count = 0;
		while(itr.hasNext()) { 
			key = (String)itr.next();
			if(key.startsWith(primaryKeyVar)) {
				++count;
			}  
		}  
		return count;
  }


  
  
}
